(require 'package)
(add-to-list 'package-archives
             '("melpa" . "http://melpa.milkbox.net/packages/") t)
(package-initialize)

(add-to-list 'load-path "~/.emacs.d/site-lisp")
;(load-file "~/.emacs.d/site-lisp/cedet-1.1/common/cedet.el")
(require 'cedet)
(require 'ecb)
(semantic-mode 1)
;(semantic-load-enable-minimum-features)
;(semantic-load-enable-code-helpers)
;(semantic-load-enable-guady-code-helpers)
;(semantic-load-enable-excessive-code-helpers)
;(semantic-load-enable-semantic-debugging-helpers)



;; ;(require 'ibus)
;; ;(add-hook 'after-init-hook 'ibus-mode-on)

;; (add-to-list 'load-path  "~/.emacs.d/site-lisp/")
;; ;; (load "~/.emacs.d/site-lisp/highlight-indentation.el")
;; ;; (set-face-background 'highlight-indentation-face "#e3e3d3")
;; ;; (set-face-background 'highlight-indentation-current-column-face "#c3b3b3")

(add-to-list 'load-path  "~/.emacs.d/site-lisp/git-emacs")
(require 'git-emacs)
(global-set-key [f9] 'git-commit-all)

(require 'linum)
(global-linum-mode 1)

(define-key global-map [f2] 'goto-line)
(global-set-key [f3] 'delete-other-windows)
(global-set-key [f5] 'revert-buffer)  ; refresh buffer 


(column-number-mode t)
(line-number-mode t)

(setq inhibit-startup-message t)

(show-paren-mode t)
(setq show-paren-style 'parentheses)



(tool-bar-mode 0)

(fset 'yes-or-no-p 'y-or-n-p)
(setq x-select-enable-clipboard t)
(global-set-key "\r" 'newline-and-indent)

(require 'bs)
(global-set-key (kbd "C-x C-b") 'bs-show)
(add-hook 'bs-mode-hook 'hl-line-mode)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; auto complete seeting ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(add-to-list 'load-path (file-name-as-directory
                         (expand-file-name "~/.emacs.d/site-lisp/auto-complete")))
(load "~/.emacs.d/site-lisp/auto-complete/auto-complete-config.el")
;; (require 'auto-complete-clang)  
;; (setq ac-clang-auto-save t)  
;; (setq ac-auto-start nil)  
;; (setq ac-quick-help-delay 0.5)
;; ;(setq ac-set-trigger-key [f4])
;; (define-key ac-mode-map  (kbd "M-p") 'auto-complete)  
;; ;; (define-key ac-mode-map  [([[/home/chengming/workspace/emacs/notes/11549fdP.png]]control tab)] 'auto-complete)  
;; (define-key ac-mode-map  [(control tab)] 'auto-complete)  

;; (defun my-ac-cc-mode-setup ()  
;;   (setq ac-sources (append '(ac-source-clang ac-source-yasnippet) ac-sources)))  
;; (add-hook 'c-mode-common-hook 'my-ac-cc-mode-setup)  

(defun proj-ac-config ( project-root)
  (setq ac-clang-cflags  
	(mapcar(lambda (item)(concat "-I" item))  
	       (split-string
		(concatenate 'string
		"/usr/include/c++/4.4  
     /usr/include/c++/4.4/i486-linux-gnu  
     /usr/include/c++/4.4/backward  
     /usr/local/include  
     /usr/lib/gcc/i486-linux-gnu/4.4.5/include  
     /usr/lib/gcc/i486-linux-gnu/4.4.5/include-fixed  
     /usr/include/i486-linux-gnu  
     /usr/include
    "
		(shell-command-to-string (concat "/home/chengming/.emacs.d/bin/listpath.sh " project-root))
		)
		)))
;  (ac-clang-update-cmdlineargs)
;  (setq-default ac-sources '(ac-source-abbrev ac-source-dictionary ac-source-words-in-same-mode-buffers))  
;  (add-hook 'emacs-lisp-mode-hook 'ac-emacs-lisp-mode-setup)  
  ;; (add-hook 'c-mode-common-hook 'ac-cc-mode-setup)  
;  (add-hook 'ruby-mode-hook 'ac-ruby-mode-setup)  
;  (add-hook 'css-mode-hook 'ac-css-mode-setup)  
;  (add-hook 'auto-complete-mode-hook 'ac-common-setup)  
;  (global-auto-complete-mode t)
  (print ac-clang-cflags)
  )  

(defun prt-ac-config ()
  (interactive)
  (print ac-clang-cflags)
  )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; org-mode ;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq org-agenda-files (list "~/workspace/pm.org"))
(setq org-todo-keywords
      '((sequence "TODO" "DOING" "FEEDBACK" "REVIEW" "|" "DONE" "DELEGATED")))
(setq org-todo-keyword-faces
      '(
        ("FEEDBACK" . (:foreground "pink" :weight bold))
	("REVIEW" . (:foreground "blue" :weight bold))
        ))
(global-set-key (kbd "C-x i") 'org-clock-in)
(global-set-key (kbd "C-x o") 'org-clock-out)
(global-set-key [f11] 'org-todo-list)

(add-hook 'org-mode-hook 'hl-line-mode)

;;;  image for org-mode
; 1. suspend current emacs window
; 2. call scrot to capture the screen and save as a file in $HOME/.emacs.img/
; 3. put the png file reference in current buffer, like this:  [[file:/home/path/.emacs.img/1q2w3e.png]]

(add-hook 'org-mode-hook 'iimage-mode)
(defun my-screenshot ()
  "Take a screenshot into a unique-named file in the current buffer file
  directory and insert a link to this file."
  (interactive)
  (setq filename
	(concat (make-temp-name
		 (concat  (getenv "HOME") "/workspace/emacs/notes/" ) ) ".png"))
  (suspend-frame)
  (call-process-shell-command "scrot" nil nil nil nil " -s " (concat
							    "\"" filename "\"" ))
  (insert (concat "[[" filename "]]"))
  (org-display-inline-images)
  )
 
(global-set-key (kbd "C-p") 'my-screenshot)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; replease and find ;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(global-set-key (kbd "C-%") 'match-paren)          
(defun match-paren (arg)
  "Go to the matching paren if on a paren; otherwise insert %."
  (interactive "p")
  (cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
	((looking-at "\\s\)") (forward-char 1) (backward-list 1))
	(t (self-insert-command (or arg 1)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; something............. ;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq stack-trace-on-error nil)

;; (require 'semantic/analyze)
;; (provide 'semantic-analyze)
;; (provide 'semantic-ctxt)
;; (provide 'semanticdb)
;; (provide 'semanticdb-find)
;; (provide 'semanticdb-mode)
;; (provide 'semantic-load)
;; (add-to-list 'load-path
;; 	     "~/.emacs.d/site-lisp/ecb-2.40")
;; (require 'ecb)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; yasnippet ;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(add-to-list 'load-path "~/.emacs.d/elpa/yasnippet-20130722.1832/snippets")
(require 'yasnippet) ;; not yasnippet-bundle
;; (yas/initialize)
(yas/load-directory "~/.emacs.d/elpa/yasnippet-20130722.1832/snippets")
(yas-global-mode 1)



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; hide show ;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(load-library "hideshow")
(add-hook 'c-mode-hook 'hs-minor-mode)
(add-hook 'c++-mode-hook 'hs-minor-mode)
(add-hook 'java-mode-hook 'hs-minor-mode)
(add-hook 'python-mode-hook 'hs-minor-mode)


(defun myhook()
   (setq indent-tabs-mode nil)
   (c-set-style "bsd")
   )
(add-hook 'c++-mode-hook 'myhook)
(add-hook 'c-mode-hook 'myhook)

(global-set-key (kbd "C-f") 'hs-toggle-hiding)
(global-set-key (kbd "C-l") 'hs-hide-level)


;; ;(add-to-list 'load-path  "~/.emacs.d/site-lisp/")
;; ;/home/chengming/.emacs.d/site-lisp/nginx-mode

;; (load "~/.emacs.d/site-lisp/nginx-mode/nginx-mode.el")

;; (setq tab-width 4)




;; ; emacs-for-python
;; ;(load-file "~/.emacs.d/site-lisp/emacs-for-python/epy-init.el")
;; ;(global-set-key "\r" 'newline-and-indent)
;; ;end emacs-for-python


(windmove-default-keybindings 'meta)  



;; ;(add-to-list 'load-path  "~/.emacs.d/site-lisp/ibus-el/")
;; ;(require 'ibus)
;; ;(add-hook 'after-init-hook 'ibus-mode-on)

(global-set-key (kbd "C-SPC") 'nil)

(require 'xcscope)

;; ;; (require 'doxymacs)
;; ;; (add-hook 'c-mode-common-hook 'doxymacs-mode)
;; ;; (defun my-doxymacs-font-lock-hook ()
;; ;;     (if (or (eq major-mode 'c-mode) (eq major-mode 'c++-mode))
;; ;;         (doxymacs-font-lock)))
;; ;; (add-hook 'font-lock-mode-hook 'my-doxymacs-font-lock-hook)



(setq indent-tabs-mode nil)

;; ;; auto complete
;; ;; -*- Emacs-Lisp -*-
 
;; ;; Time-stamp: <2010-04-09 10:22:51 Friday by ahei>
;; (add-to-list 'load-path  "~/.emacs.d/site-lisp/auto-complete-1.3.1")

;; (require 'auto-complete)
;; ;(require 'auto-complete-config)
;; (ac-config-default)

;; (add-to-list 'load-path  "~/.emacs.d/site-lisp/auto-complete-clang")
;; (require 'auto-complete-clang)
;; (setq ac-clang-auto-save t)
;; (setq ac-auto-start t)
;; (setq ac-quick-help-delay 0.5)
;; (ac-set-trigger-key "TAB")
;; (define-key ac-mode-map  [(control tab)] 'auto-complete)
;; (define-key ac-mode-map  [(control tab)] 'auto-complete)
;; (defun my-ac-config ()
;;   (setq ac-clang-flags
;;         (mapcar(lambda (item)(concat "-I" item))
;;                (split-string
;;                 "
;;  /usr/include/c++/4.4
;;  /usr/include/c++/4.4/i486-linux-gnu
;;  /usr/include/c++/4.4/backward
;;  /usr/local/include
;;  /usr/lib/gcc/i486-linux-gnu/4.4.5/include
;;  /usr/lib/gcc/i486-linux-gnu/4.4.5/include-fixed
;;  /usr/include/i486-linux-gnu
;;  /usr/include
;;  /home/chengming/.wtcfinclude/include/wtcf
;; ")))
;;   (setq-default ac-sources '(ac-source-abbrev ac-source-dictionary ac-source-words-in-same-mode-buffers))
;;   (add-hook 'emacs-lisp-mode-hook 'ac-emacs-lisp-mode-setup)
;;   ;; (add-hook 'c-mode-common-hook 'ac-cc-mode-setup)
;;   (add-hook 'ruby-mode-hook 'ac-ruby-mode-setup)
;;   (add-hook 'css-mode-hook 'ac-css-mode-setup)
;;   (add-hook 'auto-complete-mode-hook 'ac-common-setup)
;;   (global-auto-complete-mode t))
;; (defun my-ac-cc-mode-setup ()
;;   (setq ac-sources (append '(ac-source-clang ac-source-yasnippet) ac-sources)))
;; (add-hook 'c-mode-common-hook 'my-ac-cc-mode-setup)
;; ;; ac-source-gtags
;; (my-ac-config)




(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(column-number-mode t)
 '(custom-enabled-themes (quote (adwaita)))
 '(ecb-layout-window-sizes (quote (("left8" (ecb-directories-buffer-name 0.3230769230769231 . 0.11764705882352941) (ecb-sources-buffer-name 0.3230769230769231 . 0.11764705882352941) (ecb-methods-buffer-name 0.3230769230769231 . 0.5882352941176471) (ecb-history-buffer-name 0.3230769230769231 . 0.16176470588235295)))))
 '(ecb-options-version "2.40")
 '(org-agenda-files (quote ("~/workspace/pm.org")))
 '(org-tags-column -100)
 '(show-paren-mode t)
 '(tool-bar-mode nil))
; '(zjl-hl-c++-mode-enable-flag t))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(default ((t (:inherit nil :stipple nil :background "#CCE8CF" :foreground "black" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 90 :width normal :foundry "unknown" :family "DejaVu Sans Mono")))))

 ;(set-background-color "#CCE8CF")

;; ;;insert-current-date
;; (defun insert-current-date ()
;;   "Insert the current date"
;;   (interactive "*")
;;   (insert (format-time-string "%Y/%m/%d" (current-time))))
;; (global-set-key "\C-xd" 'insert-current-date)

;; (setq c-default-style
;;       '((java-mode . "java")
;; 	(awk-mode . "awk")
;; 	(other . "user")))

;; ;; (require 'ido)
;; ;; (ido-mode t)

;; (require 'rcompile)

;; ;; my emacs plugin
;; (add-to-list 'load-path  "~/.emacs.d/site-lisp/emacs-util/")
;; (require 'save-do)
;; ;(setq save-do-command "ls -all")
;; (global-set-key [f8] 'save-do)
;; (setq save-do-command "/home/chengming/workspace/CLO/rdp/clo_smgr/upload.exp")


;; (setq indent-tabs-mode nil)
;; ;; (global-hl-line-mode 1)
;; ;; (set-face-background 'hl-line "#dcdcdc")
;; ;; (set-face-attribute hl-line-face nil :underline t)
;; (ac-set-trigger-key "TAB")
;; (setq ac-auto-start nil)

;; (require 'package)
;; (add-to-list 'package-archives '("org" . "http://orgmode.org/elpa/") t)
;; (add-to-list 'package-archives '("repo" . "http://marmalade-repo.org/packages/") t)









(require 'sr-speedbar)


;; do not generate ~ files
(setq backup-by-copying t      ; don't clobber symlinks
      backup-directory-alist
      '(("." . "~/.emacs_tmp"))    ; don't litter my fs tree
      delete-old-versions t
      kept-new-versions 6
      kept-old-versions 2
      version-control t)       ; use versioned backups

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; switch between cpp and h files
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'switch-file)
(setq switch-path '("../include/" "../src/" "./")) ; will search c cpp and h in ./ , ../include and ../src

(defun switch-c ()
  (global-set-key [f1] 'switch-c-to-h)
)
(add-hook 'c-mode-hook 'switch-c)

(defun switch-cpp ()
  (global-set-key [f1] 'switch-cc-to-h)
)
(add-hook 'c++-mode-hook 'switch-cpp)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; proj ;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;


;; (require 'save-do)
;; ;(setq save-do-command "ls -all")
;; (setq save-do-command "/home/chengming/workspace/CLO/rdp/clo_smgr/upload.exp")

; do rsync

(defun proj-save-rsync ( ) 
  "Save buffers and do a command."
  (save-some-buffers)
  (shell-command proj-rsync-script )
  (interactive) 
  )

(defun proj-load (arg)
  (interactive
   (list
    (completing-read "Choose one project: " (directory-files "~/.emacs.d/projects" nil "\\.el$") )))
  (load (concat "~/.emacs.d/projects/" arg))
  ;(insert arg)
  )



(defun current-buffer-mode ( )
  "Returns the major mode associated with a buffer."
  (interactive) 
  (message "%s" major-mode)
  )


(global-set-key (kbd "C-x p") 'proj-load)
(global-set-key [f8] 'proj-save-rsync)

(global-set-key (kbd "C-= l") 'bookmark-bmenu-list)
(global-set-key (kbd "C-= s") 'bookmark-set)

;(setq split-height-threshold 0)
(setq split-width-threshold nil)

(add-hook 'cscope-list-entry-mode-hook 'hl-line-mode)
;(add-hook 'bs-mode-hook 'hl-line-mode)


(require 'auto-complete-clang-async)

(defun ac-cc-mode-setup ()
  (setq ac-clang-complete-executable "~/.emacs.d/site-lisp/clang-complete")
  (setq ac-sources '(ac-source-clang-async))
  (ac-clang-launch-completion-process)
)

(defun my-ac-config ()
  (add-hook 'c-mode-common-hook 'ac-cc-mode-setup)
  (add-hook 'auto-complete-mode-hook 'ac-common-setup)
  (global-auto-complete-mode t))

(my-ac-config)


(require 'sudo)
(global-set-key (kbd "C-c s u") 'sudo-reopen)
(require 'vtl)



(defun my-cpp-highlight ()
  "highlight c/c++ #if 0 #endif macros"
  ;; (interactive)
  (setq cpp-known-face 'default)
  (setq cpp-unknown-face 'default)
  (setq cpp-known-writable 't)
  (setq cpp-unknown-writable 't)
  (setq cpp-edit-list '(("0" font-lock-comment-face default both)
                        ("1" default font-lock-comment-face both)))
  (cpp-highlight-buffer t))

(add-hook 'c-mode-common-hook 'my-cpp-highlight)
